package com.itheima.ydd.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.*;
import com.itheima.ydd.entity.YddArticle;
import com.itheima.ydd.entity.YddCate;
import com.itheima.ydd.service.IYddArticleService;
import com.itheima.ydd.service.IYddCateService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
//分类管理
@RestController
@RequestMapping("/ydd_cate")
public class YddCateController {
    @Autowired
    private IYddCateService cateService;
    @Autowired
    private IYddArticleService articleService;

    /**
     * 展示所有标签列表
     *
     * @param catename
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/list")
    public ResultData list(String catename, int page, int limit) {
        Page<YddCate> pageInfo = new Page<>(page, limit);
        LambdaQueryWrapper<YddCate> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(catename), YddCate::getCatename, catename);
        cateService.page(pageInfo, wrapper);

        //获取对象集合
        List<YddCate> cateList = cateService.list(wrapper);
        //将数据封装为pageResult
        PageResult pageResult = PageResult.getResult(pageInfo);
        pageResult.setData(cateList);

        return ResultData.ok("success",pageResult);
    }

    /**
     * 修改时数据回显
     *
     * @param yddCate
     * @return
     */
    @GetMapping("/rowInfo")
    public ResultData rowInfo(YddCate yddCate) {
        YddCate tag = cateService.getById(yddCate.getId());
        //errmsg 为"success"时弹窗不正常
        return ResultData.ok("",tag);
    }

//TODO:未测试
    /**
     * 修改标签
     *
     * @param yddCate
     * @return
     */
    @PostMapping("/update")
    public ResultData update(@RequestBody YddCate yddCate) {
        cateService.updateById(yddCate);
        return ResultData.ok("success",yddCate);
    }

    /**
     * 删除标签
     *
     * @param id
     * @return
     */
    @GetMapping("/del")
    public R delte(String id) {
        YddCate cate = cateService.getById(id);
        if ("1".equals(cate.getDefaultData())) {
            throw new BusinessException("系统默认数据无法删除!");
        }
        LambdaQueryWrapper<YddArticle> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(YddArticle::getCateid, id);
        int count = articleService.count(wrapper);
        if (count > 0) {
            throw new BusinessException("该分类下有关联的文章,无法删除!");
        }
        cateService.removeById(id);
        return R.success("GetDone",6);
    }

    /**
     * 新增标签
     *
     * @param yddCate
     * @return
     */
    @PostMapping("/add")
    public R save(@RequestBody YddCate yddCate) {
        cateService.save(yddCate);
        return R.success("ok",666);
    }

    /**
     * 查询所有标签
     *
     * @return
     */
    @GetMapping("/all")
    public ResultData getAll() {
        List<YddCate> yddCates = cateService.list();
        return ResultData.ok("success",yddCates);
    }
}
